const NAVS = [{
    id: 0,
    name: '全部',
  },
  {
    id: 1,
    name: '团队',
  }
];
const tab = {
  all: {
    currentPage: 1,
    totalPage: 1,
    showCount: 18
  },
  team: {
    currentPage: 1,
    totalPage: 1,
    showCount: 12
  }
}
import {
  getAlls,
  getTeamRankList
} from '../../../api/weddingMan/index'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    navList: NAVS,
    activeId: 0,

    allList: [],
    teamList: [],

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    if(options.tab){
      this.setData({
        activeId: Number(options.tab)
      })
    }
  },
  onShow(){
    this.initList()
  },
  chooseNav(e) {
    let id = Number(e.currentTarget.dataset.id)
    const {
      activeId
    } = this.data
    if (id === activeId) {
      return
    }
    this.setData({
      activeId: id,
    });
    this.initList()
  },
  showPopup(e) {
    ;
  },
  initList() {
    const {
      activeId
    } = this.data
    if (activeId === 0) {
      this.initAllList()
    }
    if (activeId === 1) {
      this.initTeamList()
    }

  },
  initAllList() {
    tab['all'].totalPage = 1
    tab['all'].currentPage = 1
    this.getAllList()
    this.setData({
      allList : []
      

    })
  },
  initTeamList() {
    tab['team'].totalPage = 1
    tab['team'].currentPage = 1
    this.setData({
      teamList : []
      

    })
    this.getTeamList()
  },
  getAllList() {
    const tabId = 'all'
    getAlls({
      showCount: tab[tabId].showCount,
      currentPage: tab[tabId].currentPage,
      city: '',
      priceStart:'',
      priceEnd: '',
      ageStart: '',
      ageEnd: '',
    }).then((res) => {
      // ;
      res.data.list.forEach((item) => {
        if (item.user_headimg.indexOf('https://static.hudongmiao.com') > -1) {
          item.user_headimg =
            `https://ustatic.hudongmiao.com${item.user_headimg.split('https://static.hudongmiao.com')[1]}`
        } else if (item.user_headimg === 'undefined') {
          item.user_headimg =
            'https://ustatic.hudongmiao.com/joymewMp/defaultHeadImg2.png';
        }
      })
      let list = res.data.list || [];
      
      this.setData({
        allList: this.data.allList.concat(list)
      })
      tab[tabId].totalPage = res.data.totalPage || 1;
    }).catch((err) => {
      ;
    })
  },
  getTeamList() {
    const tabId = 'team'

				getTeamRankList({
					showCount: tab[tabId].showCount,
					currentPage: tab[tabId].currentPage,
					// queryType: '2'
				}).then((res) => {
					;
					let list = res.data.list || [];
					list.forEach((item)=>{
            item.key = `team${item.team_id}`
						item.userlist = item.userlist|| []
						item.isMutualFollow = '2'
						item.userlist  = item.userlist.filter( (item ,index) => {
							return index < 5
            }).map( (item) => {
              return {
                ...item,
                key:`member${item.userId}`

              }
            })
            
					})
          // this.teamList = this.teamList.concat(list);
          this.setData({
            teamList : this.data.teamList.concat(list)
          })
					tab[tabId].totalPage = res.data.totalPage || 1;
					// tab[tabId].showLoadMore = false;
				}).catch((err) => {
					;
				})
  },
  onReachBottom() {
    const { activeId } = this.data
    let activeTab = 'all';
    let activeFunc = this.getAllList.bind(this)
    if (activeId === 0) {
      activeTab = 'all'
      activeFunc = this.getAllList.bind(this)
    }
    if (activeId === 1) {
      activeTab = 'team'
      activeFunc = this.getTeamList.bind(this)
    }

    const {
      currentPage,
      totalPage
    } = tab[activeTab]
    
    if (currentPage >= totalPage) {
      wx.showToast({
        title: `没有更多了`,
        icon: 'none'
      })
      return ;
    } 
    // else {
      tab[activeTab].currentPage = tab[activeTab].currentPage + 1
      activeFunc();
    // }


  }
});